#include "mainwindow.h"

#include <memory>

#include <QApplication>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>

#include "sqlite_helper.h"

void SqliteTest() {
    SqliteHelper sqlite_helper;
    if (!sqlite_helper.Initialize("sqlite_test", "sqlite_test"))
    {
        qDebug() << "Initialize fail";
        return;
    }

    sqlite_helper.ExecuteCommand(std::unique_ptr<SqliteCommandInterface>{ new AddPersonCommand(10086, "leza", 24) });
    sqlite_helper.ExecuteCommand(std::unique_ptr<SqliteCommandInterface>{ new InsertPersonCommand("zhangmei", 24) });
    sqlite_helper.ExecuteCommand(std::unique_ptr<SqliteCommandInterface>{ new UpdatePersonCommand(11, "aaaa", 114514) });
    std::optional<QSqlQuery> search_opt = sqlite_helper.ExecuteQuery(std::unique_ptr<SqliteQueryInterface>{ new ReadAllFromPersonQuery() });
    if (search_opt.has_value())
    {
        QSqlQuery search = search_opt.value();
        while (search.next()) {
        qDebug() << search.value(0).toInt() << ","
                 << search.value(1).toString() << ","
                 << search.value(2).toInt();
        }
    } else {
        qDebug() << "Query erorr";
    }

    sqlite_helper.ExecuteCommand(std::unique_ptr<SqliteCommandInterface>{ new AddPersonCommand(1231, "nndt", 223) });
    search_opt = sqlite_helper.ExecuteQuery(std::unique_ptr<SqliteQueryInterface>{ new ReadNameFromPersonQuery(1231) });
    if (search_opt.has_value())
    {
        auto search = search_opt.value();
        while (search.next()) {
            qDebug() << search.value(0).toString();
        }
    } else {
        qDebug() << "Query erorr";
    }

    sqlite_helper.Close();
}

int main(int argc, char *argv[])
{
    SqliteTest();
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
